package org.fuxia.im.gui.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class FileUtil {

	static final DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	public static void writeMessage(String filePath,String message) {
		String today = sdf.format(new Date()) ;
		
		File file = new File(filePath);
		if(!file.exists()) {
			try {
				file.getParentFile().mkdirs();
				file.createNewFile() ;
			} catch (IOException e) {
			}
		}
		Map<String,String> record = readRecord(filePath);
		ObjectOutputStream oos = null ;
		try {
			if(record == null) {
				record = new HashMap<String, String>() ;
			}
			if(record.containsKey(today)) {
				message = record.get(today)+ message;
			} 
			
			record.put(today, message);
			oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(record);
			
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally{
			try {
				if(oos!=null){
					oos.close() ;
				}
			} catch (IOException e) {
			}
			
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String,String> readRecord(String filePath) {
		File file = new File(filePath);
		if(!file.exists()) {
			return null ;
		}
		ObjectInputStream ois = null ;
		try {
			ois = new ObjectInputStream(new FileInputStream(file));
			Object obj = ois.readObject() ;
			if(obj != null ){
				return (Map<String,String>)obj ;
			}
			
		}catch (Exception e) {
		} finally{
			try {
				if(ois!=null) {
					ois.close() ;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return null ;
	}
	
	public static void removeKey(String uname ,String path) {
		File file = new File(path);
		if(!file.exists()) {
			return ;
		}
		Map<String,String> info = readRecord(path);
		ObjectOutputStream oos = null ;
		try {
			if(info == null) {
				return ;
			}
			if(info.containsKey(uname)) {
				info.remove(uname);
			} 
			oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(info);
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally{
			try {
				if(oos!=null) {
					oos.close() ;
				}
			} catch (IOException e) {
			}
		}
	}
	
	public static void writeInfo(String uname,String password,String path) {
		File file = new File(path);
		if(!file.exists()) {
			try {
				file.getParentFile().mkdirs();
				file.createNewFile() ;
			} catch (IOException e) {
			}
		}
		Map<String,String> info = readRecord(path);
		ObjectOutputStream oos = null ;
		try {
			if(info == null) {
				info = new HashMap<String, String>() ;
			}
			if(info.containsKey(uname) && info.get(uname).equals(password)) {
				return ;
			} 
			if(info.containsKey(uname)) {
				info.remove(uname);
			}
			info.put(uname, password);
			oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(info);
			
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally{
			try {
				if(oos!=null) {
					oos.close() ;
				}
			} catch (IOException e) {
			}
		}
	}
	
	public static void main(String[] args) {
		String filePath = "d:\\a\\b\\c\\d.txt" ;
		File f = new File(filePath);
		f.getParentFile().mkdirs();
		try {
			f.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
